package com.bitdf.txing.product.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.product.entity.CategoryEntity;
import com.bitdf.txing.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;



/**
 * 商品三级分类
 *
 * @author lizhiwei
 * @email sunlightcs@gmail.com
 * @date 2022-05-16 20:22:24
 */
@RestController
@RequestMapping("${txing.request.path.product}/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出三级分页
     */
    @RequestMapping("/list/tree")
    public R list(){
        List<CategoryEntity> entities=categoryService.listWithTree();
        return R.ok().put("data",entities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:category:save")
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 更新三级分类排序 批量更新
     * @param category
     * @return
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:category:update:sort")
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category)
    {
        categoryService.updateBatchById((Arrays.asList(category)));
        return R.ok();
    }

    /**
     * 修改三级分类
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:category:update")
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:category:delete")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
